<?php
include_once 'mod.php';
$mod = new mod();
$mysql = $mod->link();
$id = $_GET['id'];
$sql1 = "select * from classification";
$result1 =$mod->select($mysql,$sql1);
$sql = "select g.*,c.class_name as c_id from goods g inner join classification c on g.class = c.id where g.id = '$id'";
$result = $mod->select($mysql,$sql);
$class_id = $result[0]['class'];
if ($_SERVER['REQUEST_METHOD']=='POST'){
    $id = $_POST['id'];
    $good_name = $_POST['good_name'];
    $class = $_POST['class'];
    $style = $_POST['style'];
    $update_time = date('Y-m-d H:i:s');
    $where = 'where';
    $sql = "update goods set";
    if (!empty($good_name)){
        $sql .=" `good_name` = '$good_name',";
        $where = 'and';
    }
    if (!empty($class)){
        $sql .=" `class` = $class,";
        $where = 'and';
    }
    if (!empty($style)){
        $sql .=" `style` = $style,";
        $where = 'and';
    }
    if (!empty($update_time)){
        $sql .=" `update_time` = '$update_time' where id = $id";
        $where = 'and';
    }
    $result = $mod->IDU($mysql,$sql);
    if ($result['flag']){
        die('修改成功<a href="index1.php">返回首页</a>');
    }else{
        print_r($sql);
        die('修改失败'.$result['msg']. '<a href="index1.php">返回首页</a>');
    }
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>修改</title>
</head>
<body>
<form action="" method="post">
    <p>商品名:<input type="text" name="good_name" value="<?=$result[0]['good_name']?>"></p>
    <p>
        类型:<select name="class">
            <option value="">===请选择类型===</option>
            <?php foreach ($result1 as $item){?>
            <option value="<?=$item['id']?>" <?=$class_id==$item['id']?'selected':''?>><?=$item['class_name']?></option>
            <?php }?>
        </select>
    </p>
    <p>
        状态:<input type="radio" name="style" value="1" <?=$result[0]['style']==1?'checked':''?>>在售
        <input type="radio" name="style" value="2" <?=$result[0]['style']==2?'checked':''?>>售罄
    </p>
    <p><input type="hidden" name="id" value="<?=$result[0]['id']?>"></p>
    <input type="submit" value="更改">
</form>
</body>
</html>
